<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="安全检查员id" prop="safetyInspectorId">
      <el-input v-model="dataForm.safetyInspectorId" placeholder="安全检查员id"></el-input>
    </el-form-item>
    <el-form-item label="楼栋名" prop="name">
      <el-input v-model="dataForm.name" placeholder="楼栋名"></el-input>
    </el-form-item>
    <el-form-item label="楼栋地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="楼栋地址"></el-input>
    </el-form-item>
    <el-form-item label="建筑面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="建筑面积"></el-input>
    </el-form-item>
    <el-form-item label="建筑高度" prop="height">
      <el-input v-model="dataForm.height" placeholder="建筑高度"></el-input>
    </el-form-item>
    <el-form-item label="层数" prop="floorsNumber">
      <el-input v-model="dataForm.floorsNumber" placeholder="层数"></el-input>
    </el-form-item>
    <el-form-item label="建筑结构，如钢筋混凝土(砼)结构" prop="buildingStructure">
      <el-input v-model="dataForm.buildingStructure" placeholder="建筑结构，如钢筋混凝土(砼)结构"></el-input>
    </el-form-item>
    <el-form-item label="防火等级" prop="fireRating">
      <el-input v-model="dataForm.fireRating" placeholder="防火等级"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          safetyInspectorId: '',
          name: '',
          address: '',
          area: '',
          height: '',
          floorsNumber: '',
          buildingStructure: '',
          fireRating: ''
        },
        dataRule: {
          safetyInspectorId: [
            { required: true, message: '安全检查员id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '楼栋名不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '楼栋地址不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '建筑面积不能为空', trigger: 'blur' }
          ],
          height: [
            { required: true, message: '建筑高度不能为空', trigger: 'blur' }
          ],
          floorsNumber: [
            { required: true, message: '层数不能为空', trigger: 'blur' }
          ],
          buildingStructure: [
            { required: true, message: '建筑结构，如钢筋混凝土(砼)结构不能为空', trigger: 'blur' }
          ],
          fireRating: [
            { required: true, message: '防火等级不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$request({
              url: `/house/building/info/${this.dataForm.id}`,
              method: 'get',
              params: {}
            }).then((res) => {
              if (res && res.code === 0) {
                this.dataForm.safetyInspectorId = res.building.safetyInspectorId
                this.dataForm.name = res.building.name
                this.dataForm.address = res.building.address
                this.dataForm.area = res.building.area
                this.dataForm.height = res.building.height
                this.dataForm.floorsNumber = res.building.floorsNumber
                this.dataForm.buildingStructure = res.building.buildingStructure
                this.dataForm.fireRating = res.building.fireRating
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$request({
              url: `/house/building/${!this.dataForm.id ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'id': this.dataForm.id || undefined,
                'safetyInspectorId': this.dataForm.safetyInspectorId,
                'name': this.dataForm.name,
                'address': this.dataForm.address,
                'area': this.dataForm.area,
                'height': this.dataForm.height,
                'floorsNumber': this.dataForm.floorsNumber,
                'buildingStructure': this.dataForm.buildingStructure,
                'fireRating': this.dataForm.fireRating
              }
            }).then((res) => {
              if (res && res.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
